package com.eagle.videoboot.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfigurer
 *
 * @author eagle liu
 * @date 2018-11-30
 */
@Configuration
@EnableWebMvc
@Slf4j
public class ConfigurerAdapter implements WebMvcConfigurer {

    @Value("${file.avatar}")
    private String avatar;

	@Bean(initMethod="init")
	public ZKCuratorClient zkCuratorClient() {
		return new ZKCuratorClient();
	}

	//拦截token
	@Bean
	public VideoBootInterceptor videoBootInterceptor() {
		return new VideoBootInterceptor();
	}

	//需要拦截的地址
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		registry.addInterceptor(videoBootInterceptor()).addPathPatterns("/user/**")
				.addPathPatterns("/video/upload", "/video/uploadCover",
						"/video/userLike", "/video/userUnLike",
						"/video/saveComment")
				.addPathPatterns("/bgm/**")
				.excludePathPatterns("/user/queryPublisher");

//		super.addInterceptors(registry);
	}

	//外部资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String avatarUtl = "file:" + avatar.replace("\\","/");
//		avatarUtl += "/"; //for linux
        log.info("avatarUtl path is {}", avatarUtl);
		registry
				.addResourceHandler("/**").addResourceLocations(avatarUtl)
				.addResourceLocations("classpath:/META-INF/resources/");
    }
}
